import {fileURLToPath, URL} from 'node:url'

import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import ViteCompressionPlugin from 'vite-plugin-compression'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
import visualizer from "rollup-plugin-visualizer";
import importToCDN from 'vite-plugin-cdn-import'
// https://vitejs.dev/config/
export default defineConfig({
    server: {
        proxy: {
            '/api': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ''),
            },
        }
    },
    plugins: [
        vue(),
        AutoImport({
            resolvers: [ElementPlusResolver()],
        }),
        Components({
            resolvers: [ElementPlusResolver()],
        }),
        ViteCompressionPlugin({
            filter: /\.(js|css)$/i, // 只压缩 js 和 css 文件
            threshold: 1024, // 只压缩大于1KB的文件
            algorithm: 'gzip', // 使用 gzip 算法
            ext: '.gz', // 压缩文件的扩展名
        }),
        importToCDN({// echarts走CDN
            modules: [
                {
                    name: 'echarts',
                    var: 'echarts',
                    path: 'https://cdn.bootcdn.net/ajax/libs/echarts/5.4.2/echarts.min.js',
                },
            ],
        }),
        visualizer() // 放到最后
    ],
    resolve: {
        alias: {
            // 定义一个别名 '@'，该别名对应于当前 JavaScript 模块文件所在目录下的 'src' 目录的绝对文件路径。
            '@': fileURLToPath(new URL('./src', import.meta.url))
        }
    }
})
